var express = require('express')
var config = require('./config/index')
const proxy = require('http-proxy-middleware'); // 引入代理中间件
var port = process.env.PORT || config.build.port

var app = express()

var router = express.Router()
// app.get('/', function (req, res, next) {
//   console.log(req.url)

//   req.url = '/index.html'
//   next()
// })
// app.get('/index', function (req, res, next) {
//   req.url = '/index.html'
//   next()
// })
// app.get('/loan', function (req, res, next) {
//   req.url = '/index.html'
//   next()
// })
// router.get('*', function (req, res, next) {
//   console.log(req.url)
//   req.url = '/index.html'
//   next()
//   res.render('404', {
//     status: 404,
//     title: 'NodeBlog'
//   })
//   next()
// })
let reg = /\.(png|jpe?g|gif|svg|js|css|woff2?|eot|ttf|otf)(\?.*)?$/
router.get('*', function (req, res, next) {
  console.log(req.url)
  if (!reg.test(req.url)) {
    req.url = '/index.html'
  }
  next()
})
// const apiProxy = proxy('/api', { target: 'https://ifci.lincomb.com', changeOrigin: true }); // 将服务器代理到localhost:8080端口上[本地服务器为localhost:3000]
// app.use('/api/*', apiProxy); // api子目录下的都是用代理
app.use(router)

app.use(express.static('./dist'))
// app.get('*', function(req, res){
//     res.render('404.html', {
//         title: 'No Found'
//     })
// })
module.exports = app.listen(port, function (err) {
  if (err) {
    console.log(err)
    return
  }
  console.log('Listening at http://localhost:' + port + '\n')
})
